package com.imay.live.manager;

import android.os.Handler;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.imay.live.fragment.dynamics.DynamicsFragment;
import com.imay.live.helper.EventHelper;
import com.imay.live.model.UserModel;
import com.imay.tbas_api.IMYTBASObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chan on 2017/5/25 0025.
 */

public class DynamicsNotifyManager {
    private static DynamicsNotifyManager instance;

    private IMYTBASObject imytbasObject;

    private List<Long> roomids;

    public static DynamicsNotifyManager getInstance() {
        if (instance == null) {
            instance = new DynamicsNotifyManager();
        }
        return instance;
    }

    public List<Long> getRoomids() {
        if (roomids == null) {
            roomids = new ArrayList<>();
        }
        return roomids;
    }

    /**
     * 添加socket 监听
     */
    public void registerDynamicNotifyListener(final Handler dynamicsFragmentHandler) {
        //监听socket
        imytbasObject = new IMYTBASObject(false);
        imytbasObject.stringCommand = EventHelper.EVENT_DYNAMICS_NOTIFY;
        imytbasObject.setDataResultListener(new IMYTBASObject.OnDataResultListener() {
            @Override
            public void onCommandTaskSuccess(IMYTBASObject object) {
                UserModel protoResult = JSON.parseObject(object.stringData, UserModel.class);
                if (roomids == null) {
                    roomids = new ArrayList<Long>();
                }
                if (!roomids.contains(protoResult.getRoomId())) {
                    roomids.add(protoResult.getRoomId());
                }
                dynamicsFragmentHandler.obtainMessage(DynamicsFragment.ACTION_6).sendToTarget();
                //dynamicNotifyListener.onLiveNotify(roomids.size());
            }
        });
        imytbasObject.addEventListenWithTBASObject();
    }

    public void unregisterDynamicNotifyListener() {
        if (imytbasObject != null) {
            imytbasObject.destroy();
        }
    }

    public interface DynamicNotifyListener {
        void onLiveNotify(int size);
    }
}
